<?php

namespace app\admin\controller;

use app\common\service\UpyunService;
use think\facade\Env;

class Common extends Base
{

	/**
     * 生成token函数
     * @Author zhibin
     * @Date   2019-11-26
     * @return [type]     [description]
     */
    public function getToken()
    {
        $request = \think\Request::instance();
        $token = $request->token();

        return json(['code'=>1, 'msg'=>1, 'data'=>['token'=>$token]]);
    }

    /**
     * 开放式图片上传
     * @Author   zhibin
     * @DateTime 2020-02-19
     * @return   [type]     [description]
     */
    public function upload()
    {
        $file = request()->file('file');
        // pre($file);die;
        if($file){
            $root_path = Env::get('root_path');
            $dir = $root_path. 'public' .DIRECTORY_SEPARATOR. 'uploads' .DIRECTORY_SEPARATOR. 'imgs';
            $info = $file->rule('uniqid')->move($dir); //保存原图
            if ($info===false) {
                return json(['code'=>0, 'msg'=>$file->getError()]);
            }
            $file_info = $info->getInfo();
            $file_path = $info->getSaveName();
            // pre($fileInfo);die;

            $upload_type = config('upload_type');
            if ($upload_type == 2) {
                $dir_image = $info->getPathname();
                $UpyunService = new UpyunService();
                $file_path = $UpyunService->upload($dir_image);
                $file_title = $fileInfo['name'];
                unset($info);
                unlink($dir_image);
            }

            return json(['code'=>1, 'msg'=>'Success', 'data'=>[
                'file_path' => $file_path,
                'file_name' => $file_info['name'],
            ]]);
        }else{
            return json(['code'=>0, 'msg'=>'Fail', 'data'=>[]]);
        }
    }

    /**
     * 上传到阿里云oss
     * @Author binx
     * @Date   2022-02-28
     * @return [type]     [description]
     */
    public function uploadoss()
    {   
        $file = request()->file('file');
        $error = error_get_last(); 
        if ($error['type']==2) {
            ob_get_clean(); //关闭Warning提示
            return json([
                'code'=>-1,
                'msg'=>'上传文件超出'.ini_get('upload_max_filesize').'限制'
            ]);
        }
        if (empty($file)) {
            return json([
                'code'=>-1,
                'msg'=>'请选择文件'
            ]);
        }
        $oss_config = config('UPLOAD_OSS_CONFIG');
        $ossClient = new \OSS\OssClient($oss_config['AccessKeyId'],$oss_config['AccessKeySecret'],$oss_config['Endpoint']);
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
        $object = date('Y/m/d/') . substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
        $res = $ossClient->uploadFile($oss_config['Bucket'], $object, $filePath);

        if($res === null) {
            return $this->ajaxReturn(0, '上传文件失败！', []);
        }else {
            $result = [
                'path'=>$object,
                'name'=>$file->getInfo('name'),
                'size'=>$res['info']['size_upload'],
                'url'=>$oss_config['domain'].'/'.$object,
            ];
            return $this->ajaxReturn(1, 'Success', $result);
        }
    }

     /**
     * 清理缓存
     * @Author zhibin
     * @Date   2021-07-14
     * @return [type]     [description]
     */
    public function clearcache()
    {
        if (class_exists('Redis')) {
            $Redis = \org\MyRedis::getInstance(config('redis'))->getRedis();

            $rkey = input('get.key', '', 'trim');
            if (!empty($rkey)) {
                $res = $Redis->del($rkey);
                $result = $res ? '成功' : '失败';
                echo "{$result}清理key为 {$rkey} 缓存<br/>\n";
            } else {
                $objClass = new \ReflectionClass(\org\MyRedis::class);
                $constant_arr = $objClass->getConstants();
                // $property_arr = $objClass->getProperties();
                // $method_arr = $objClass->getMethods();
                foreach ($constant_arr as $key => $rkey) {
                    $res = $Redis->del($rkey);
                    $result = $res ? '成功' : '失败';
                    echo "{$result}清理key为 {$rkey} 缓存<br/>\n";
                }
            }
        }
    }


}